package cn.stylefeng.guns.sys.modular.dataShift.service.impl;

import cn.stylefeng.guns.sys.modular.dataManagement.model.params.StandardManagementParam;
import cn.stylefeng.guns.sys.modular.dataManagement.model.result.StandardManagementResult;
import cn.stylefeng.guns.sys.modular.dataQualityCheck.entity.EtlJob;
import cn.stylefeng.guns.sys.modular.dataQualityCheck.model.params.QualityCheckParam;
import cn.stylefeng.guns.sys.modular.dataQualityCheck.service.QualityCheckService;
import cn.stylefeng.guns.sys.modular.dataShift.mapper.DataShiftMapper;
import cn.stylefeng.guns.sys.modular.dataShift.service.DataShiftService;
import cn.stylefeng.guns.sys.modular.dbServicelist.model.params.DbServicelistParam;
import cn.stylefeng.guns.sys.modular.dbServicelist.model.result.DbServicelistResult;
import cn.stylefeng.guns.sys.modular.dbservicedetails.model.params.DbServicedetailsParam;
import cn.stylefeng.guns.sys.modular.dbservicedetails.model.result.DbServicedetailsResult;
import cn.stylefeng.guns.sys.modular.metadata.mapper.MetadataMapper;
import cn.stylefeng.guns.sys.modular.metadata.model.result.MetadataResult;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class DataShiftServiceImpl implements DataShiftService {

    @Autowired
    DataShiftMapper dataShiftMapper;

    @Autowired
    MetadataMapper metadataMapper;

    @Override
    public int dataShift(Integer metadataId) {
        //判断是否检查成功的标志
        int isTrue = 0;
        Integer serviceId = dataShiftMapper.searchServiceId(metadataId);

        //注册申请的时候需要抓取数据
        //将申请的数据放到etl_job表中
        EtlJob etlJob = new EtlJob();
        Random random = new Random();
        int y = 0;
        String x = "";
        do {
            y = random.nextInt(1000000000);
            x = "j" + String.valueOf(y);
            etlJob.setJobId(x);
        }
        while (dataShiftMapper.searchEtlJob(etlJob).size() > 0);
        etlJob.setMetadataId(metadataId.toString());
        etlJob.setToDsId("0");
        etlJob.setFromDsId(serviceId.toString());
        etlJob.setJobName(metadataId + "的数据转换");
        dataShiftMapper.insertEtlJob(etlJob);

        //获取到刚添加到etl_job表中的id
        List<EtlJob> listEtlJob = dataShiftMapper.searchEtlJob(etlJob);
        //获取数据
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httppost
        //TODO 部署的时候这个地方要改成调用迁数据项目的地址
        HttpPost httppost = new HttpPost("http://172.16.0.157:9000/ETLEngine/api/job/startJOb");
        // 创建参数队列
        List<BasicNameValuePair> formParams = new ArrayList<BasicNameValuePair>();
        formParams.add(new BasicNameValuePair("jobid", listEtlJob.get(0).getJobId()));
        //runType为4的时候是数据转换
        formParams.add(new BasicNameValuePair("runType", "4"));
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
            httppost.setEntity(uefEntity);
            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    System.out.println("--------------------------------------");
                    System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
                    System.out.println("--------------------------------------");
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            isTrue = 1;
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            isTrue = 1;
            e1.printStackTrace();
        } catch (IOException e) {
            isTrue = 1;
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //如果检查成功
        if (isTrue == 0) {
            isTrue = 200;
        }
        //如果检查失败删除任务
        if (isTrue == 1) {
            dataShiftMapper.deleteEtlJob(etlJob);
            isTrue = 500;
        }
        return isTrue;
    }
}
